<?php
/**
 * Created by PhpStorm.
 * User: chenx
 * Date: 18-3-9
 * Time: 上午11:41
 */

namespace shopend\controllers;
use common\models\City;
use common\models\Code;
use common\models\Shop;
use common\models\Tree;
use common\service\ConstantService;
use common\service\GlobalUrlService;


class ShopController extends BaseController
{

    /**
     * 添加
     */
    public function actionAddShop(){
        if($this->isAjax() && $this->isPost()){
            $data = $this->post();

            $re_code = $this->_checkcode($data['admin_phone'],$data['code']);

            if(!is_numeric($re_code)){
                return $this->renderJson([],$re_code,ConstantService::$response_fail);
            }

            $msg = $this->_validation($data,true);
            if($msg !== true){
                return $this->renderJson([],$msg,ConstantService::$response_fail);
            }
            $model = new Shop();
            $data['password'] = md5(md5($data['password']).$this->salt);
            $data['status'] = 0;
            $data['create_time'] = time();
            $data['update_time'] = time();
            $model->setAttributes($data);
            if($model->save()){
                $Codes = Code::findOne(['id'=>$re_code]);
                $Codes->is_del = 1;
                $Codes->save();
                return $this->renderJson([],'入驻申请成功!待审核',ConstantService::$response_success);
            }else{
                return $this->renderJson([],'新增失败',ConstantService::$response_fail);
            }
        }
        return $this->render('add-shop');
    }

    /**
     * 修改
     */
    public function actionUpdateShop(){
        if($this->isPost() && $this->isAjax()){
            $data = $this->post();
            $re_code = $this->_checkcode($data['admin_phone'],$data['code']);

            if(!is_numeric($re_code)){
                return $this->renderJson([],$re_code,ConstantService::$response_fail);
            }
            $msg = $this->_validation($data,false);
            $data['customer_phone'] = $data['customer_phone']?$data['customer_phone']:$data['admin_phone'];
            if($msg != 'true'){
                return $this->renderJson([],$msg,ConstantService::$response_fail);
            }
            if(!empty($data['password'])){
                $data['password'] = md5(md5($data['password']).$this->salt);
            }else{
                unset($data['password']);
            }
            $shop_model = Shop::findOne(['id'=>$data['id']]);

            $data['completed_at'] = time();
            $data['status'] = 0;
            $shop_model->setAttributes($data);
            if($shop_model->save()){
                $Codes = Code::findOne(['id'=>$re_code]);
                $Codes->is_del = 1;
                $Codes->save();
                return $this->renderJson([],'重新提交成功!待审核',ConstantService::$response_success);
            }
            return $this->renderJson([],'修改失败',ConstantService::$response_fail);
        }
        $admin_phone = $this->get('admin_phone');
        $data = Shop::find()->where(['admin_phone'=>$admin_phone])->asArray()->one();
        $code = $this->get('code');
        $this->_checkcode($admin_phone,$code);
        $trees = Tree::find()->where(['pid'=>0])->asArray()->all();
        return $this->renderPartial('update-shop',['data'=>$data,'trees'=>$trees]);
    }


    /**
     *查看
     */
    public function actionGetInfo(){
        $admin_phone = $this->post('admin_phone');
        $data = Shop::find()->where(['admin_phone'=>$admin_phone])->asArray()->one();
        if($data){
            return $this->renderJson($data['id'],'ok',ConstantService::$response_success);
        }
        return $this->renderJson([],'该账号未入驻',ConstantService::$response_fail);
    }

    /**
     * 获取省
     */
    public function actionGetProvince(){
        if($this->isAjax() && $this->isPost()){
            $data = City::find()->where(['city_id'=>0])->asArray()->all();
            return $this->renderJson(['data'=>$data],'',ConstantService::$response_success);

        }
        return $this->renderJson([],'接口错误',ConstantService::$response_fail);

    }
    /**
     * 获取区
     */
    public function actionGetCity(){
        if($this->isAjax() && $this->isPost()){
            $id = $this->post('id');
            $data = City::find()->where(['province_id'=>$id,'area_id'=>0])->asArray()->all();
            return $this->renderJson($data,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'接口错误',ConstantService::$response_fail);
    }
    /**
     * 获取县
     */
    public function actionGetCounty(){
        if($this->isAjax() && $this->isPost()){
            $id = $this->post('id');
            $data = City::find()->where(['city_id'=>$id])->asArray()->all();
            return $this->renderJson($data,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'接口错误',ConstantService::$response_fail);
    }
    /**
     * 获取单个
     */
    public function actionGetOne(){
        if($this->isAjax() && $this->isPost()){
            $id = $this->post('id');
            $data = City::find()->where(['id'=>$id])->asArray()->one();
            return $this->renderJson($data,'',ConstantService::$response_success);
        }
        return $this->renderJson([],'接口错误',ConstantService::$response_fail);

    }

    /**
     * 验证填入字段是否正确
     * @param array $data
     * @param bool $type
     * @return string
     */
    private function _validation($data = array(),$type = true){

        if(empty($data['shop_name'])){
            return "店铺名称不能为空";
        }
        if(empty($data['user_account'])){
            return "店铺登录账号不能为空";
        }
        if(!preg_match('/^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|17[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$/',$data['admin_phone'])) {
            return '请输入正确的管理员手机号';
        }
        if(empty($data['administration'])){
            return "店铺管理姓名不能为空";
        }
        if(!preg_match('/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/',$data['admin_email'])) {
            return '请输入正确的管理员邮箱';
        }

        if(empty($data['join_card_no'])){
            return "店铺管理人身份证不能为空";
        }
        if(empty($data['join_card1_pic'])){
            return "店铺管理人身份证正面照不能为空";
        }
        if(empty($data['join_card2_pic'])){
            return "店铺管理人身份证反面照不能为空";
        }

        if(empty($data['join_card3_pic'])){
            return "店铺管理人手持身份证照片不能为空";
        }

        $data_shop_name = Shop::findOne(['shop_name'=>$data['shop_name']]);
        $data_user_account = Shop::findOne(['user_account'=>$data['user_account']]);
        $data_admin_phone = Shop::findOne(['admin_phone'=>$data['admin_phone']]);
        $customer_phone = Shop::findOne(['customer_phone'=>$data['customer_phone']]);

        if($type){
            //新增
            if(!empty($data_shop_name)){
                return "已经存在同名店铺";
            }
            if(!empty($data_user_account)){
                return "已经存在同名登录账号";
            }
            if(!empty($data_admin_phone)){
                return "已经存在同名管理员电话号码";
            }
            if(empty($data['password'])){
                return "店铺管理密码不能为空";
            }
            if(!empty($customer_phone)){
                return "商家客服电话已存在";
            }
        }else{
            //修改
            if(!empty($data_shop_name) && $data['id'] != $data_shop_name['id']){
                return "已经存在同名店铺";
            }
            if(!empty($data_user_account) && $data['id'] != $data_user_account['id']){
                return "已经存在同名登录账号";
            }
            if(!empty($data_admin_phone) && $data['id'] != $data_admin_phone['id']){
                return "已经存在同名管理员电话号码";
            }
            if(!empty($customer_phone) && $data['id'] != $customer_phone['id']){
                return "已经存在同名管理员电话号码";
            }

        }
        return true;
    }

    /**
     * 验证验证码是否正确
     * @param $phone
     * @param $code
     * @return string
     */
    private function _checkcode($phone,$code){
        if(empty($phone)){
            return '管理员手机号不能为空!';
        }
        if( empty($code)){
            return '验证码不能为空!';
        }
        $data = Code::find()->where(['phone'=>$phone])->orderBy(['id'=>SORT_DESC])->asArray()->one();
        if((time()-$data['time'])>300 || $data['is_del'] == 1){
            return '验证码错误';
        }
        if($data['code'] == $code){
            return $data['id'];
        }else{
            return '验证码错误';
        }
    }

    /**
     * 修改logo
     */
    public function actionModifyLogo(){
        if($this->isPost() && $this->isAjax()){
            $shop_id = $this->shop['id'];
            $shop_model = Shop::findOne(['id'=>$shop_id]);
            $logo = $this->post('logo');
            $shop_model->shop_logo = $logo;
            if($shop_model->save()){
                return $this->renderJson([],'修改成功',ConstantService::$response_success);
            }
            return $this->renderJson([],'修改失败',ConstantService::$response_fail);
        }
        return $this->renderJson([],'参数错误',ConstantService::$response_fail);
    }

    /**
     * 商家详情
     * @return string
     */
    public function actionShopInfo(){
        $data = Shop::find()->where(['id'=>$this->shop['id']])->asArray()->one();
        return $this->render('shop-info',['row'=>$data]);
    }
}